<?php
/**
 * User: Jerry
 * Date: 2017/5/24
 * Time: 11:22
 */

namespace SDXApi\Model;
use Think\Model;

/**
 * Class CommunityTemplateGoodsModel.class
 * @author Jerry
 * @package SDXApi\Model
 */
class CommunityTemplateGoodsModel extends Model
{
    /**
     * @var string
     */
    protected $tableName = 'community_goods';

    /**
     * @author Jerry
     * @param int $template_id
     * @return mixed
     */
    public function findGoodsListByTemplateId($template_id = 0, $wid = 8)
    {
        $where = array(
            'sg.template_id'	=>	$template_id,
            'g.is_delete'	    =>	0,
            'g.is_on_sale'	    =>	1,
            'g.original_img'    =>	array(
                'NEQ',
                ''
            ),
        );

        $field = 'sg.goods_id,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.delivery_cycle,gs.practical_num,sg.sort';

        $data = $this
            ->alias('sg')
            ->field($field)
            ->join('LEFT JOIN __GOODS_WID_'. $wid .'__ AS g ON sg.goods_id = g.goods_id')
            ->join('LEFT JOIN __GOODS_STORAGE__ AS gs ON sg.goods_id = gs.goods_id AND gs.wid = ' . $wid)
            ->where($where)
            ->group('sg.goods_id')
            ->select();

        return $data;
    }
}